<?php
require_once("db_functions.php");
require_once("security.class.php");
$sec = new security();
if(!$sec->isUserLoggedIn()){
	header("Location:index.php");
}
$db = new db_functions();
$a = $_GET["a"];
$limit = $_GET["l"];
switch($a){
	case "radnici";
		$v = $_GET["v"];
		$q = "SELECT radnik_id, radnik_ime, radnik_prezime FROM radnici WHERE radnik_ime LIKE '%".$v."%' OR radnik_prezime LIKE '%".$v."%' ORDER BY radnik_ime ";
		$res = $db->select2($q);
		if(!$res){
			die();
		}
		foreach($res as $v){
			echo $v["radnik_id"].";".$v["radnik_ime"]." ".$v["radnik_prezime"]."\n";
		}
		break;
	case "komitenti":
		$v = $_GET["v"];
		$q = "SELECT komitent_id,komitent_name FROM komitenti WHERE komitent_name LIKE '".$v."%' ORDER BY komitent_name";
		if($limit){
			$q .= " LIMIT $limit";
		}
		$res = $db->select2($q);
		if(!$res){
			die();
		}
		foreach($res as $k=>$v){
			echo $v["komitent_id"].";".$v["komitent_name"];
			if($k<count($res)-1){
				echo "\n";
			}
		}
		break;
	case 'artikli':
		$type = $type?$type:-1;
		$q = "SELECT * FROM artikli";
		if($type>0)
			$q .= " WHERE art_grupa_id=".$type;
		$q .= " ORDER BY art_name";
		$res = $db->select2($q);
		$out="";
		foreach($res as $k=>$v){
			$out .= "{!!}".$v["art_id"]."{!}".$v["art_name"]."{!}".$v["art_desc"];
		}
		echo substr($out,4);
		break;
	case 'price':
		$art_id = $art;
		$art_tip = $tip;
		$art_color = $col;
		$art_unit = $un;
		$datum_ugovora = $d;
		
		$tmp = explode(".",$datum_ugovora,3);
		$dat_day = $tmp[0];
		$dat_month = $tmp[1];
		$dat_year = $tmp[2];
		
		$datum_ugovora = makeUnixTime(0,0,0,$dat_month,$dat_day,$dat_year);
		$res = $db->select2("SELECT * FROM cenovnici ORDER BY cenovnik_start DESC");
		foreach($res as $v){
			if($v["cenovnik_start"] < $datum_ugovora){
				$cenovnik_id = $v["cenovnik_id"];
				break;
			}
		}
		$artikl = $db->getOne("SELECT art_name FROM artikli WHERE art_id=".$art_id);
		
		
		$q = "SELECT cena_value,cena_art_tip,cena_art_boja,cena_unit_id "
			."FROM cene "
			."WHERE cena_art_id=".$art_id." AND cena_cenovnik=".$cenovnik_id;
		if($art_tip){
			$q .= " AND cene.cena_art_tip=".$art_tip;
		}
		if($art_color){
			$q .= " AND cene.cena_art_boja=".$art_color;
		}
		if($un){
			$q .= " AND cene.cena_unit_id=".$un;
		}
		$q .= " ORDER BY cena_value ASC";
		$res = $db->select1($q);
		if($res){
			$out="";
			foreach($res as $v){
			
				$tax = 0.18;
				$price = $v[0]*(1+$tax);
				
				$tip_id = $v[1];
				$boja_id=$v[2];
				$unit_id = $v[3];
				$tip_name = $db->getOne("SELECT arttip_name FROM artikli_tipovi WHERE arttip_id=".$tip_id);
				$q="SELECT art_boja_name, art_boja_code FROM artikli_boje WHERE art_boja_id=".$boja_id;
				$res = $db->select1($q);
				$res = $res[0];
				$boja_name = $res[0];
				$boja_code = $res[1];
				$unit_name = $db->getOne("SELECT unit_svalue FROM jedinice_mere WHERE unit_id=".$unit_id);
				$out .= $artikl."{!}".$price."{!}".$tip_name."{!}".$boja_code."{!}".$unit_name."\n";
			}
			echo $out;
		}else{
			echo "-1 , $q";
		}
		break;
	case 'opstine':
		$regid = $_GET["id"];
		$sql = "SELECT opstina_id, opstina_name FROM opstine WHERE opstina_region_id=".$regid." ORDER BY opstina_name";
		$res = $db->select2($sql);
		if($res){
			$out = "";
			foreach($res as $v){
				$out .= "{!!}".$v["opstina_id"]."{!}".$v["opstina_name"];
			}
			$out = substr($out,4);
			echo $out;
		}
		break;
	case 'lokacija':
		$t = $_GET["t"];
		$id = $_GET["id"];
		switch($t){
			case 'mesta':
				$sql = "SELECT lid as id, lok_naziv as ime, lok_ptt FROM lokacije WHERE lok_parent=$id AND lok_tip=1";
				break;
			case 'opstine':
				$sql = "SELECT lid as id, lok_naziv as ime, lok_ptt FROM lokacije WHERE lok_parent=$id AND lok_tip=2";
				break;
			case 'naselja':
				$sql = "SELECT lid as id, lok_naziv as ime, lok_ptt FROM lokacije WHERE lok_parent=$id AND lok_tip=3";
				break;
		}
		$res = $db->select2($sql);
		if($res){
			$out = "";
			foreach($res as $v){
				$out .= "{!!}";
				$line="";
				foreach($v as $m=>$n){
					$line .= "{!}".$n;
				}
				$out .= substr($line,3);
			}
			echo substr($out,4);
		}
		break;
	case 'ulice':
		$v = $_GET["v"];
		$sql = "SELECT * FROM ulice WHERE ulica_ime like '".$v."%' ORDER BY ulica_ime";
		if($limit){
			$sql .= " LIMIT $limit";
		}
		$res = $db->select2($sql);
		if(!$res){
			die();
		}
		foreach($res as $k=>$v){
			if($v["ulica_id"]>0){
				echo $v["ulica_id"].";".$v["ulica_ime"];
				if($k<count($res)-1){
					echo "\n";
				}
			}
		}
		break;
	default:
		echo "Unknown action: ".$a;
}
?>